﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;

using DBUtility;

namespace BLL
{
    public class User
    {
        DAL.UserTableAdapters.UserTableAdapter DU =
            new DAL.UserTableAdapters.UserTableAdapter();

        MODEL.User _MU = new MODEL.User();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MU"></param>
        /// <returns></returns>
        public MODEL.User GetDataByID(MODEL.User MU)
        {
            try
            {
                DataTable DT = DU.GetDataByID(MU.User_ID);

                _MU.User_Address = DT.Rows[0]["User_Address"].ToString();
                _MU.User_DateTime = Convert.ToDateTime(DT.Rows[0]["User_DateTime"]);
                _MU.User_Email = DT.Rows[0]["User_Email"].ToString();
                _MU.User_ID = Convert.ToInt32(DT.Rows[0]["User_ID"]);
                _MU.User_IsLock = Convert.ToBoolean(DT.Rows[0]["User_IsLock"]);
                _MU.User_LoginID = DT.Rows[0]["User_LoginID"].ToString();
                _MU.User_LoginIP = DT.Rows[0]["User_LoginIP"].ToString();
                _MU.User_PassAnswer = DT.Rows[0]["User_PassAnswer"].ToString();
                _MU.User_PassQuestion = DT.Rows[0]["User_PassQuestion"].ToString();
                _MU.User_Password = DT.Rows[0]["User_Password"].ToString();
                _MU.User_Phone = DT.Rows[0]["User_Phone"].ToString();
                _MU.User_LoginDateTime = Convert.ToDateTime(DT.Rows[0]["User_LoginDateTime"]);

                return _MU;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 用户注册
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="MU"></param>
        public void Add(MODEL.User MU)
        {
            try
            {
                DataTable DT_Check = DU.GetDataByLoginID(MU.User_LoginID);

                if (DT_Check.Rows.Count > 0)
                {
                    throw new Exception("此用户已被注册");
                }

                DU.Add(MU.User_LoginID,
                    MU.User_Password,
                    MU.User_Email,
                    MU.User_Phone,
                    MU.User_Address,
                    MU.User_PassQuestion,
                    MU.User_PassAnswer,
                    MU.User_DateTime,
                    MU.User_LoginIP,
                    MU.User_IsLock,
                    MU.User_LoginDateTime);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 用户登陆
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="MU"></param>
        public int UserLogin(MODEL.User MU)
        {
            try
            {
                DataTable DT_UserLogin = DU.GetDataByLoginID_Pass(MU.User_LoginID, MU.User_Password);

                if (DT_UserLogin.Rows.Count == 0)
                {
                    throw new Exception("用户名或者密码错误");
                }
                else if (DT_UserLogin.Rows.Count > 1)
                {
                    throw new Exception("用户名数据异常");
                }

                int UserID = Convert.ToInt32(DT_UserLogin.Rows[0]["User_ID"]);

                if (Convert.ToBoolean(DT_UserLogin.Rows[0]["User_IsLock"]))
                {
                    throw new Exception("此用户已被锁定");
                }

                DU.ModIP_TimeForUserLogin(MU.User_LoginIP, MU.User_LoginDateTime, UserID);

                return UserID;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除用户
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="MU"></param>
        public void Del(MODEL.User MU)
        {
            try
            {
                DU.Del(MU.User_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记用户锁定/激活状态
        /// <summary>
        /// 标记用户锁定/激活状态
        /// </summary>
        /// <param name="MU"></param>
        public void MarkIsLock(MODEL.User MU)
        {
            try
            {
                string SQLstr = "UPDATE [User] SET [User_IsLock]=NOT [User_IsLock] WHERE [User_ID]=" + MU.User_ID;

                AccHelper.ExecuteSQL(SQLstr);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}
